package com.nineclock.auth.exception;

import com.fasterxml.jackson.core.JsonGenerator;
import com.fasterxml.jackson.databind.SerializerProvider;
import com.fasterxml.jackson.databind.ser.std.StdSerializer;

import java.io.IOException;

/*
    自定义序列化器
 */
public class NcOAuthExceptionJacksonSerializer extends StdSerializer<NcOAuth2Exception> {

    //无参构造
    public NcOAuthExceptionJacksonSerializer(){
        super(NcOAuth2Exception.class);
    }

    /**
     *  {
     *      "success" : false
     *      "code" : ""
     *      "message" ""
     *  }
     */
    //重写方法
    @Override
    public void serialize(NcOAuth2Exception e, JsonGenerator jsonGenerator, SerializerProvider serializerProvider) throws IOException {

        jsonGenerator.writeStartObject();

        jsonGenerator.writeObjectField("success",false);
        jsonGenerator.writeObjectField("code",e.getHttpErrorCode());
        jsonGenerator.writeObjectField("message",e.getMessage());

        jsonGenerator.writeEndObject();
    }
}